package com.jacky.configeration;

import com.jacky.entity.SayHelloProperties;
import com.jacky.service.SayHelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration//开启配置
@EnableConfigurationProperties(SayHelloProperties.class)//开启使用映射实体对象
@ConditionalOnClass(SayHelloService.class)//存在HelloService时初始化该配置类
@ConditionalOnProperty//存在对应配置信息时初始化该配置类
        (
                prefix = "say.hello",//存在配置前缀hello
                value = "enabled",//开启
                matchIfMissing = true//缺失检查
        )
public class SayHelloAutoConfiguration {
        //application.properties配置文件映射前缀实体对象
        @Autowired
        private SayHelloProperties sayHelloProperties;

        /**
         * 根据条件判断不存在HelloService时初始化新bean到SpringIoc
         * @return
         */
        @Bean//创建HelloService实体bean
        @ConditionalOnMissingBean(SayHelloService.class)//缺失HelloService实体bean时，初始化HelloService并添加到SpringIoc
        public SayHelloService helloService()
        {
                System.out.println(">>>The HelloService Not Found，Execute Create New Bean.");
                SayHelloService helloService = new SayHelloService();
                helloService.setMsg(sayHelloProperties.getMsg());//设置消息内容
                helloService.setShow(sayHelloProperties.isShow());//设置是否显示
                return helloService;
        }

}
